package com.pw.tool.utils.pinyin;

import com.github.promeg.pinyinhelper.Pinyin;
import com.github.promeg.pinyinhelper.PinyinMapDict;
import com.github.promeg.tinypinyin.lexicons.java.cncity.CnCityDict;

import java.util.HashMap;
import java.util.Map;


public class TinyPinyinUtil {
    static {
        Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance()));// 添加中文城市词典
        // 添加自定义词典
        Pinyin.init(Pinyin.newConfig()
                .with(new PinyinMapDict() {
                    @Override
                    public Map<String, String[]> mapping() {
                        HashMap<String, String[]> map = new HashMap<>();
                        map.put("重庆", new String[]{"CHONG", "QING"});
                        map.put("中牟县", new String[]{"ZHONG", "MU", "XIAN"});
                        return map;
                    }
                }));
    }
    //汉语转大写拼音
    public static String toUpperPinyin(String str, String separator){
        return Pinyin.toPinyin(str,separator);
    }

    //汉语转大写拼音
    public static String toUpperPinyin(String str){
        return Pinyin.toPinyin(str,"");
    }

    //汉语转小写拼音
    public static String toLowerPinyin(String str, String separator){
        return Pinyin.toPinyin(str,separator).toLowerCase();
    }

    //汉语转小写拼音
    public static String toLowerPinyin(String str){
        return Pinyin.toPinyin(str,"").toLowerCase();
    }

    //测试方法
    public static void main(String[] args) {
        System.out.println(toUpperPinyin("中牟县"));//ZHONGMUXIAN
        System.out.println(toLowerPinyin("中牟县"));//zhongmuxian
    }
}
